package org.like;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

public class Order {
    private String orderId;
    private String customerId;
    private String productName;
    private double price;
    private int quantity;
    private String category;
    private LocalDate orderDate;

    // 构造方法
    public Order(String orderId, String customerId, String productName,
                 double price, int quantity, String category, LocalDate orderDate) {
        this.orderId = orderId;
        this.customerId = customerId;
        this.productName = productName;
        this.price = price;
        this.quantity = quantity;
        this.category = category;
        this.orderDate = orderDate;
    }

    // Getter方法
    public String getOrderId() { return orderId; }
    public String getCustomerId() { return customerId; }
    public String getProductName() { return productName; }
    public double getPrice() { return price; }
    public int getQuantity() { return quantity; }
    public double getTotalPrice() { return price * quantity; } // 新增计算订单总价方法
    public String getCategory() { return category; }
    public LocalDate getOrderDate() { return orderDate; }

    @Override
    public String toString() {
        return productName + " x" + quantity + " ($" + getTotalPrice() + ")";
    }

    public static void main(String[] args) {
        List<Order> orders = List.of(
                new Order("O001", "C101", "无线耳机", 199.9, 2, "电子产品", LocalDate.of(2023, 5, 10)),
                new Order("O002", "C102", "运动水杯", 45.0, 3, "生活用品", LocalDate.of(2023, 5, 15)),
                new Order("O003", "C101", "机械键盘", 320.0, 1, "电子产品", LocalDate.of(2023, 6, 2)),
                new Order("O004", "C103", "Java编程书", 89.0, 1, "图书", LocalDate.of(2023, 6, 5)),
                new Order("O005", "C104", "充电宝", 120.0, 2, "电子产品", LocalDate.of(2023, 6, 10)),
                new Order("O006", "C102", "马克杯", 35.0, 4, "生活用品", LocalDate.of(2023, 6, 12)),
                new Order("O007", "C105", "台灯", 75.5, 1, "生活用品", LocalDate.of(2023, 6, 15))
        );

        // 1. 找出所有电子产品类（"电子产品"）的订单
        System.out.println("=== 电子产品订单 ===");
        List<Order> electronicsOrders = orders.stream()
                .filter(order -> "电子产品".equals(order.getCategory()))
                .collect(Collectors.toList());
        electronicsOrders.forEach(System.out::println);

        // 2. 计算2023年6月份的所有订单总销售额
        System.out.println("\n=== 2023年6月总销售额 ===");
        double juneTotalSales = orders.stream()
                .filter(order -> order.getOrderDate().getMonthValue() == 6 && order.getOrderDate().getYear() == 2023)
                .mapToDouble(Order::getTotalPrice)
                .sum();
        System.out.println("2023年6月总销售额: " + juneTotalSales);

        // 3. 获取购买数量大于等于2的所有商品名称列表（去重）
        System.out.println("\n=== 购买数量>=2的商品名称 ===");
        List<String> productNamesWithQuantityGE2 = orders.stream()
                .filter(order -> order.getQuantity() >= 2)
                .map(Order::getProductName)
                .distinct()
                .collect(Collectors.toList());
        productNamesWithQuantityGE2.forEach(System.out::println);

        // 4. 找出订单总额最高的订单信息
        System.out.println("\n=== 订单总额最高的订单 ===");
        Optional<Order> highestValueOrder = orders.stream()
                .max(Comparator.comparing(Order::getTotalPrice));
        highestValueOrder.ifPresent(order -> System.out.println("最高订单: " + order));

        // 5. 按顾客分组统计每个顾客的总消费金额
        System.out.println("\n=== 每个顾客的总消费金额 ===");
        Map<String, Double> customerTotalSpent = orders.stream()
                .collect(Collectors.groupingBy(Order::getCustomerId,
                        Collectors.summingDouble(Order::getTotalPrice)));
        customerTotalSpent.forEach((customerId, total) ->
                System.out.println("顾客 " + customerId + ": " + total));

        // 6. 按商品分类统计每类商品的平均单价（保留两位小数）
        System.out.println("\n=== 每类商品的平均单价 ===");
        Map<String, Double> averagePriceByCategory = orders.stream()
                .collect(Collectors.groupingBy(Order::getCategory,
                        Collectors.averagingDouble(Order::getPrice)))
                .entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        e -> Math.round(e.getValue() * 100.0) / 100.0
                ));
        averagePriceByCategory.forEach((category, avgPrice) ->
                System.out.println(category + ": " + avgPrice));

        // 7. 找出购买了超过2种不同商品的顾客ID（去重）
        System.out.println("\n=== 购买了超过2种不同商品的顾客 ===");
        List<String> customersWithMoreThan2Products = orders.stream()
                .collect(Collectors.groupingBy(Order::getCustomerId,
                        Collectors.mapping(Order::getProductName, Collectors.toSet())))
                .entrySet().stream()
                .filter(entry -> entry.getValue().size() > 2)
                .map(Map.Entry::getKey)
                .distinct()
                .collect(Collectors.toList());
        if (customersWithMoreThan2Products.isEmpty()) {
            System.out.println("没有顾客购买超过2种不同商品");
        } else {
            customersWithMoreThan2Products.forEach(System.out::println);
        }

        // 8. 按月份分组统计每个月的销售总额（月份格式：yyyy-MM）
        System.out.println("\n=== 每月销售总额 ===");
        Map<String, Double> monthlySales = orders.stream()
                .collect(Collectors.groupingBy(
                        order -> order.getOrderDate().format(DateTimeFormatter.ofPattern("yyyy-MM")),
                        Collectors.summingDouble(Order::getTotalPrice)));
        monthlySales.forEach((month, total) ->
                System.out.println(month + ": " + total));
    }
}
